package com.yezong.AidMe.controller;

import com.yezong.AidMe.config.Config;
import com.yezong.AidMe.entity.CtUser;
import com.yezong.AidMe.entity.Result;
import com.yezong.AidMe.entity.User;
import com.yezong.AidMe.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.logging.Logger;


@RequestMapping("user")
@RestController
public class UserController {

    @Autowired
    private UserService userService;
    Logger logger = Logger.getLogger("UserController");
    private Result result;


    @PostMapping("login")
    @ResponseBody
    public Result login(HttpSession session,@RequestBody User user){

        if (user == null ||
            user.getPhonenumber() == null ||
            user.getPhonenumber().trim().length() == 0 ||
            user.getPassword() == null ||
            user.getPassword().trim().length() == 0) {
            logger.info("调用 UserController 的 login 函数,nothing");
            //登录失败，回到登录页面，并且显示错误信息
            result = new Result(Config.STATUS_FAILURE, "手机号和密码不能为空");
//            page = "forward:/";
//            page = "redirect:/";
        } else {

            logger.info("调用 UserController 的 login 函数");

            User user1 = userService.login(user);

            boolean login = user1 != null;

            if (login) {
                session.setAttribute("user", user1);
            }

            result = new Result(login ? Config.STATUS_SUCCESS : Config.STATUS_FAILURE, login ? "登录成功" : "登录失败", login ? user1 : null);

        }
        return result;
    }

    @PostMapping("/toRegister")
    public String toRegister() { return "html/user/register";}

    @PostMapping("register")
    @ResponseBody
    public Result register(@RequestBody User user){

        logger.info("调用 UserController 的 register 函数");

        boolean register = userService.register(user);

        result = new Result(Config.STATUS_SUCCESS, "注册成功！", user);

        return result;
    }

    @RequestMapping("queryById")
    public String query(Integer uid){
        User user = userService.selectOneById(uid);
        logger.info("QureyById" + (user != null ? user.toString() : null));
        return user != null ? "getMovieList" : "failure";
    }

//    @RequestMapping("/toCertify")
//    public String toCertify() { return "html/user/certify";}

    @PostMapping("certify")
    @ResponseBody
    public Result certify(HttpSession session,@RequestBody CtUser ctUser){

        logger.info("调用 UserController 的 certify 函数");

        boolean certify = userService.certify(ctUser);
        User user = (User) session.getAttribute("user");
        System.out.println(user.toString());
        if (certify) {
            userService.updateStudentCertifiedInfo(ctUser.getNumber());
            userService.updateUserCertifiedInfo(user.getId(),ctUser.getNumber());
        }

        result = new Result(certify ? 1 : 0 ,certify ? "认证成功" : "认证失败", certify ? user : null);

        return result;
    }

}
